package com.example.gui;

import com.example.dao.AdminDAO;
import com.example.model.Deposit;
import com.example.model.UserInfo;
import com.example.util.DepositTableModel;
import com.example.util.UserTableModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;

public class DepositManagementPanel extends JPanel {


    static AdminDAO adminDAO = new AdminDAO();

    DepositTableModel tableModel;
    JTable depositTable;
    List<Deposit> depositData;

    public DepositManagementPanel() {

        depositData = fetchDepositData();
        tableModel = new DepositTableModel(depositData);
        depositTable = new JTable(tableModel);

        // 设置表格外观
        depositTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        depositTable.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
        depositTable.setRowHeight(30);
        depositTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        setLayout(new BorderLayout());
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 标题
        JLabel titleLabel = new JLabel("存款产品管理", JLabel.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        add(titleLabel, BorderLayout.NORTH);


        depositTable.setFont(new Font("宋体", Font.PLAIN, 14));
        depositTable.setRowHeight(30);
        depositTable.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));

        add(new JScrollPane(depositTable), BorderLayout.CENTER);

        // 底部按钮
        // 底部按钮
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));

        JButton addButton = createStyledButton("添加业务", Color.decode("#4CAF50"));
        JButton deleteButton = createStyledButton("删除业务", Color.decode("#F44336"));
        JButton refreshButton = createStyledButton("刷新数据", Color.decode("#FF9800"));

        buttonPanel.add(addButton);
        buttonPanel.add(deleteButton);
        buttonPanel.add(refreshButton);

        // 添加按钮事件处理
        addButton.addActionListener(this::addDeposit);
        deleteButton.addActionListener(this::deleteDeposit);
        refreshButton.addActionListener(e -> refreshData());

        add(buttonPanel, BorderLayout.SOUTH);

    }


    public static JButton createStyledButton(String text, Color bgColor) {
        JButton button = new JButton(text);
        button.setFont(new Font("微软雅黑", Font.BOLD, 14));
        button.setBackground(bgColor);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setPreferredSize(new Dimension(120, 40));
        return button;
    }

    private List<Deposit> fetchDepositData() {
        List<Deposit> data = adminDAO.getAllDeposit();
        return data;
    }

    private void addDeposit(ActionEvent e) {
        // 创建添加用户对话框
        JPanel formPanel = new JPanel(new GridLayout(5, 2, 10, 10));

        JTextField nameField = new JTextField();
        JTextField describeField = new JTextField();


        formPanel.add(new JLabel("存款名称:"));
        formPanel.add(nameField);
        formPanel.add(new JLabel("描述:"));
        formPanel.add(describeField);

        int result = JOptionPane.showConfirmDialog(
                this,
                formPanel,
                "添加新存款业务",
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.PLAIN_MESSAGE
        );

        if (result == JOptionPane.OK_OPTION) {


            String name = nameField.getText().trim();
            String describe = describeField.getText().trim();

            if (!name.isEmpty() && !describe.isEmpty()) {
                Deposit newDeposit = new Deposit();
                newDeposit.setSavingId(getNextDepositId());
                newDeposit.setSavingName(name);
                newDeposit.setDescription(describe);

                // 更新数据库
                if(!adminDAO.addNewDeposit(newDeposit)){
                    JOptionPane.showMessageDialog(this, "添加失败，请查看信息后重试!", "出错", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }

                depositData.add(newDeposit);
                tableModel.fireTableDataChanged();
                JOptionPane.showMessageDialog(this, "新存款业务添加成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "存款名称和描述不能为空!", "输入错误", JOptionPane.ERROR_MESSAGE);
            }
        }

    }


    private void deleteDeposit(ActionEvent e) {
        int selectedRow = depositTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请选择要删除的用户!", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        Deposit deposit = depositData.get(selectedRow);
        int confirm = JOptionPane.showConfirmDialog(
                this,
                "确定要删除存款业务 " + deposit.getSavingName() + " 吗?",
                "确认删除",
                JOptionPane.YES_NO_OPTION
        );

        if (confirm == JOptionPane.YES_OPTION) {

            if(!adminDAO.deleteDeposit(deposit.getSavingId())){
                JOptionPane.showMessageDialog(this, "业务删除失败!", "错误", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            depositData.remove(selectedRow);
            tableModel.fireTableRowsDeleted(selectedRow, selectedRow);
            JOptionPane.showMessageDialog(this, "业务已删除!", "成功", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    private void refreshData() {
        depositData.clear();
        depositData.addAll(fetchDepositData());
        tableModel.fireTableDataChanged();
        JOptionPane.showMessageDialog(this, "数据已刷新!", "成功", JOptionPane.INFORMATION_MESSAGE);
    }


    private int getNextDepositId() {
        int maxId = 0;
        for (Deposit deposit : depositData) {
            if (deposit.getSavingId() > maxId) {
                maxId = deposit.getSavingId();
            }
        }
        return maxId + 1;
    }



}
